package cn.sdormitory.controller;

import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.utils.StringUtils;
import cn.sdormitory.service.AppSdStudentLeaveService;
import cn.sdormitory.smartdor.entity.SdLeave;
import cn.sdormitory.smartdor.vo.SdStudentLeaveVo;
import cn.sdormitory.workflow.entity.StuLeaveApply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2021/11/15 23:31
 * @version：V1.0
 */
@RestController
@RequestMapping("/app/StudentLeave")
@Api("APP端请假管理接口")
public class AppSdStudentLeaveController {
    @Autowired
    private AppSdStudentLeaveService appSdStudentLeaveService;

    @ApiOperation("createUpdateStuLeaves => 新增/修改学生请假信息")
    @PostMapping(value = "/createUpdateStuLeaves")
    public CommonResult<Integer> createUpdateStuLeaves(@RequestBody SdStudentLeaveVo sdStudentLeaveVo) throws IOException {
        int count = appSdStudentLeaveService.addStudentLeave(sdStudentLeaveVo);
        if (count > 0) {
            return CommonResult.success(count);
        }
        if(count == -1){
            return CommonResult.failed(501,"提交的请假有重复，你确认后再提交。");
        }
        return CommonResult.failed();

    }

    @ApiOperation("deleteStuLeaves => 删除学生请假信息")
    @PostMapping(value = "/deleteStuLeaves")
    public CommonResult<Integer> deleteStuLeaves(@RequestBody Map<String,String> param) throws IOException {
        Long leaveId = Long.valueOf(param.get("leaveId"));
        String studentNo = param.get("studentNo");
        String startDate = param.get("startDate");
        String endDate = param.get("endDate");

        int count = appSdStudentLeaveService.deleteStuLeave(leaveId,studentNo,startDate,endDate);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("getStuLeavesList => 查询学生请假信息")
    @PostMapping(value = "/getStuLeavesList")
    public CommonResult<List> getStuLeavesList(@RequestBody SdLeave sdLeave) throws IOException {
        if(StringUtils.isEmpty(sdLeave.getUserName())){
            return CommonResult.failed("userName Can't be null");
        }
        List<SdLeave> List = appSdStudentLeaveService.getStuLeavesList(sdLeave);
        return CommonResult.success(List);

    }

    @ApiOperation("updateStuLeaves => 修改学生请假信息")
    @PostMapping(value = "/updateStuLeaves")
    public CommonResult<Integer> updateStuLeaves(@RequestBody SdLeave sdLeave) throws IOException {
        if(StringUtils.isEmpty(sdLeave.getUpdateDesc())){
            return CommonResult.failed("updateDesc Can't be null");
        }
        if(StringUtils.isArray(sdLeave.getId())){
            return CommonResult.failed("id Can't be null");
        }
        int count = appSdStudentLeaveService.updateStuLeaves(sdLeave);
        return CommonResult.success(count);
    }

    @ApiOperation("getStuLeaveApplyByClsTeacherId => 获取班主任审批学生请假列表")
    @PostMapping("/getStuLeaveApplyByClsTeacherId")
    public CommonResult getStuLeaveApplyByClsTeacherId(@RequestBody StuLeaveApply param){
        Long clsTeacherId = param.getClsTeacherId();
        String status = param.getStatus();
        List<Map<String, Object>> data = appSdStudentLeaveService.getStuLeaveApplyByClsTeacherId(clsTeacherId, status);
        return CommonResult.success(data);
    }

    @ApiOperation("updateStuLeaveApplyStatus => 审批学生请假")
    @PostMapping("/updateStuLeaveApplyStatus")
    public CommonResult updateStuLeaveApplyStatus(@RequestBody StuLeaveApply apply){
        int flag = appSdStudentLeaveService.updateStuLeaveApplyStatus(apply);
        if (flag > 0) {
            return CommonResult.success(flag);
        }
        if(flag == -1){
            return CommonResult.failed(501,"提交的请假有重复，你确认后再提交。");
        }
        if(flag == -2){
            return CommonResult.failed(502,"审批时间晚于请假日期。");
        }
        if (flag == -3) {
            return CommonResult.failed(502,"查找不到请假单信息,请退出重试。");
        }
        return CommonResult.failed();
    }
    
    @ApiOperation("撤销请假")
    @PostMapping("/cancelStuLeaveApply")
    public CommonResult cancelStuLeaveApply(@RequestBody StuLeaveApply stuLeaveApply) {
        appSdStudentLeaveService.cancelStuLeaveApply(stuLeaveApply);
        return CommonResult.success();
    }
    
}
